<?php
include("GeoIP/geoip.php");
class Cellphonezone_Ipcountry_Helper_Data extends Mage_Core_Helper_Abstract{

    protected $country_code = null;
    protected $country_name = null;
    public function loadCountry(){
       $gi = geoip_open("lib/GeoIP/GeoIP.dat",GEOIP_STANDARD);
       $ip = $_SERVER['REMOTE_ADDR'];
       //$ip = '72.14.203.103';
        // 获取国家代码
        $this->country_code = geoip_country_code_by_addr($gi, $ip);
        //Mage::log($this->country_code); 

        // 获取国家名称
        $this->country_name = geoip_country_name_by_addr($gi, $ip);
        //Mage::log($this->country_name); 
        // close the database
        geoip_close($gi);
    }
    public function getCountryCode(){
        if(is_null($this->country_code)){
             $this->loadCountry(); 
        }
        return $this->country_code;
    }
    public function getCountryName(){
        if(is_null($this->country_name)){
             $this->loadCountry(); 
        }
        return $this->country_name;
    }
    public function isActiveCountry(){
    
      $collection = Mage::getModel('directory/country')->getResourceCollection()
          ->loadByStore();
       return in_array($this->getCountryCode(),$collection->getColumnValues('country_id'));  
    }
}
